#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2021/8/10 14:31
# @Author    :
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from testcases.login_test import TestLogin as MubuLogin


class TestCaseMubuCreateDoc(HttpRunner):

    @pytest.mark.parametrize(
        "param",
        Parameters(
            # 多组数据取笛卡尔积
            {
                "folderId": ["1SPp0ofUxlp", "75iU3d1Xplp"],
                "memberId": "${get_member_id_list()}"
            }
        )
    )
    def test_start(self, param):
        super().test_start(param)

    config = (
        Config("幕布测试")
            .verify(False)
            # 设置base_url,只对相对路径生效
            .base_url("https://api2.mubu.com")
            # 定义全局变量，变量的引用$var_name/${var_name}，作用域：当前测试类中
            .variables(**{
            "host": "${get_host()}",
            "origin": "https://mubu.com",
            "memberId": "8786352375767376",
            "phone": "18367157420",
            "password": "qwer128201209q@",
            "folderId": "1SPp0ofUxlp",
            "id": "${gen_random_string(10)}",
            "data_unique_id": "6e2b8b65-5f01-4930-b0b4-3bfbe8edbb0d",
            'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
        })
    )

    teststeps = [
        Step(
            RunTestCase("login")
            .with_variables(**{
                "phone": "18367157420",
                "password": "qwer128201209q@"
            })
            .call(MubuLogin)
            .export("token")
        ),
        # Step(
        #     RunRequest("/v3/api/list/create_folder")
        #         .post("/v3/api/list/create_folder")
        #         .with_headers(
        #         **{
        #             "content-length": "41",
        #             "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
        #             "sec-ch-ua-mobile": "?0",
        #             "user-agent": "$user_agent",
        #             "data-unique-id": "$data_unique_id",
        #             "content-type": "application/json;charset=UTF-8",
        #             "accept": "application/json, text/plain, */*",
        #             "jwt-token": "$token",
        #             "x-request-id": "${random_md5()}",
        #             "version": "3.0.0-2.0.0.1812",
        #             "origin": "$origin",
        #             "sec-fetch-site": "same-site",
        #             "sec-fetch-mode": "cors",
        #             "sec-fetch-dest": "empty",
        #             "referer": "$host",
        #             "accept-encoding": "gzip, deflate, br",
        #             "accept-language": "zh-CN,zh;q=0.9",
        #         }
        #     )
        #         .with_json({"name": "测试文件夹111", "folderId": "0"})
        #         .extract()
        #         .with_jmespath("body.data.folder.id", "folderId")
        #         .validate()
        #         .assert_equal("status_code", 200)
        #         .assert_equal("body.code", 0)
        # ),
        Step(
            RunRequest("/v3/api/list/create_doc")
                .post("/v3/api/list/create_doc")
                .with_headers(
                **{
                    "content-length": "34",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "$user_agent",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${random_md5()}",
                    "version": "3.0.0-2.0.0.1812",
                    "origin": "$origin",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "$host",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
                .with_json({"folderId": "$folderId", "type": 0})
                .extract()
                .with_jmespath("body.data.id", "docId")
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("body.code", 0)
        ),
        Step(
            RunRequest("/v3/api/colla/events")
                .post("/v3/api/colla/events")
                .with_headers(
                **{
                    "content-length": "163",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "$user_agent",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${random_md5()}",
                    "origin": "$origin",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "$host",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
                .with_json(
                {
                    "memberId": "$memberId",
                    "type": "CHANGE",
                    "version": 0,
                    "documentId": "$docId",
                    "events": [
                        {"name": "nameChanged", "title": "${gen_random_string(10)}", "original": ""}
                    ],
                }
            )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("body.code", 0)
        ),
        Step(
            RunRequest("/v3/api/colla/events")
                .post("https://api2.mubu.com/v3/api/colla/events")
                .with_headers(
                **{
                    "content-length": "250",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "$user_agent",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${random_md5()}",
                    "origin": "$origin",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "$host",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
                .with_json(
                {
                    "memberId": "$memberId",
                    "type": "CHANGE",
                    "version": 1,
                    "documentId": "$docId",
                    "events": [
                        {
                            "name": "create",
                            "created": [
                                {
                                    "index": 0,
                                    "parentId": None,
                                    "node": {
                                        "id": "$id",
                                        "text": "",
                                        "modified": "${get_timestamp()}",
                                        "children": [],
                                    },
                                    "path": ["nodes", 0],
                                }
                            ],
                        }
                    ],
                }
            )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("body.code", 0)
        ),
        Step(
            RunRequest("/v3/api/colla/events")
                .post("/v3/api/colla/events")
                .with_headers(
                **{
                    "content-length": "454",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "$user_agent",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${random_md5()}",
                    "origin": "$origin",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "$host",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
                .with_json(
                {
                    "memberId": "$memberId",
                    "type": "CHANGE",
                    "version": 2,
                    "documentId": "$docId",
                    "events": [
                        {
                            "name": "update",
                            "updated": [
                                {
                                    "updated": {
                                        "id": "$id",
                                        "text": "<span>${gen_random_string(10)}</span>",
                                        "modified": "${get_timestamp()}",
                                    },
                                    "original": {
                                        "id": "$id",
                                        "text": "",
                                        "modified": "${get_timestamp()}",
                                    },
                                    "path": ["nodes", 0],
                                }
                            ],
                        },
                    ],
                }
            )
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("body.code", 0)
        ),
        Step(
            RunRequest("/v3/api/list/get_all_documents_page")
                .post("/v3/api/list/get_all_documents_page")
                .with_headers(
                **{
                    "content-length": "12",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "$user_agent",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "$token",
                    "x-request-id": "${random_md5()}",
                    "version": "3.0.0-2.0.0.1812",
                    "origin": "$origin",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "$host",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
                .with_json({"start": ""})
                # $response: 固定写法，获取响应的结果,get_folder_num是在debugtalk.py
                .teardown_hook("${get_folder_num($response)}", "folders")
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("body.code", 0)
                .assert_greater_or_equals("$folders", 1)
        ),
    ]


if __name__ == '__main__':
    TestCaseMubu().test_start()
